package com.datn.utils.places;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.datn.utils.DecodePolyline;
import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.text.Html;
import android.widget.Toast;

public class JsonAnyPlace {

	Context mContext;
	public JsonAnyPlace(Context context){
		mContext = context;
	}

	private String getURL(String nameAddress){		
		//http://maps.googleapis.com/maps/api/directions/json?origin=16.065444,108.183231&waypoints=16.071383,108.191321&sensor=false
		//http://maps.googleapis.com/maps/api/directions/json?origin=52.31,16.71&destination=51.27,6.75&sensor=false
		//http://maps.googleapis.com/maps/api/geocode/json?address=Tran+Cao+Van&sensor=false
		StringBuilder urlString = new StringBuilder(); 
		urlString.append("http://maps.googleapis.com/maps/api/geocode/json?"); 
		urlString.append("address="+nameAddress.replace(" ", "+"));
		urlString.append("&sensor=false");
		return urlString.toString();
	}

	private static String getResponseFromUrl(URL url) 
	{
		try {
			URLConnection urlConnection = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
			StringBuilder strBuilder = new StringBuilder();
			String temp;
			while ((temp = in.readLine()) != null) {
				strBuilder.append(temp);
			}
			return strBuilder.toString();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		return null;
	}


	public ArrayList<Place> getListAnyAddress(String nameAddress){
		ArrayList<Place> allPlace = new ArrayList<Place>();
		try {
			URL urlJSON = new URL(getURL(nameAddress));
			String jsonResponse = getResponseFromUrl(urlJSON);
			if (jsonResponse!=null){
				JSONObject jsonObject = new JSONObject(jsonResponse);

				//Process JSON HERE
				JSONArray jsonRoutes=jsonObject.getJSONArray("results");				
				if (jsonRoutes!=null){
					for (int i = 0; i < jsonRoutes.length(); i++) {
						Place place = new Place();
						try{
						JSONObject result = (JSONObject)jsonRoutes.get(i);
						String stAddress = result.getString("formatted_address");
							place.setAddressLine(stAddress);
							place.setName(nameAddress);
						JSONObject geometry = result.getJSONObject("geometry");
							JSONObject location = geometry.getJSONObject("location");
							double lat = location.getDouble("lat");
							double lng = location.getDouble("lng");
							place.setLatitude(lat);
							place.setLongtitude(lng);
							allPlace.add(place);
						}
						catch (Exception ex){
							
						}
						
					}
				}

				
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			Toast.makeText(mContext, "URL is not valid", Toast.LENGTH_LONG).show();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Toast.makeText(mContext, "Cannot parse JSON received", Toast.LENGTH_LONG).show();
		}
		
		return allPlace;
	}



}
